<?php

namespace App\Library\Logic;

use App\Models\OperateLogModel;
use Illuminate\Support\Facades\Route;

class OperateLogLogic extends OperateLogModel
{
    /**
     * Desc paging
     *
     * @param array|string|int|null $objectType
     * @param array|string|int|null $ext
     * @return void
     */
    public static function add(int|null $userId = null, array|string|int|null $objectType = null, array|string|int|null $ext = null): void
    {
        if (is_int($objectType)) {
            $ext = [$objectType];
            $objectType = null;
        } else if (is_array($objectType)) {
            $ext = $objectType;
            $objectType = null;
        } else if (is_int($ext) || is_string($ext)) $ext = [$ext];
        else if (is_null($ext) || $ext === 1) $ext = [];
        $route = explode('.', Route::currentRouteName());
        if (count($route) === 3 && in_array($route[2], ['index', 'create', 'edit'])) return;
        $userId = !is_null($userId) ?? USERID;
        if ($userId === 0) return;
        self::insertForModel([
            'userId' => $userId,
            'objectType' => $objectType ?? Route::currentRouteName(),
            'extend' => array_merge(['request' => request()->except(__('operate_log_filter'))], $ext)
        ]);
    }
}
